<?php
	
	session_start();
	
	// check for login
	if(!isset($_SESSION['atbat']) && !isset($_COOKIE['remember_atbat'])) {
	
		// no login, move to index page
		header("Location: http://localhost/atbat/html/");
	
	}
	
	// connect to db
	require_once '../db/db.php';
	
	// get user id
	$sql = "SELECT user_id FROM users WHERE MD5(username) IN ('";
	$sql .= ((isset($_SESSION['atbat'])) ? $_SESSION['atbat'] : '') . "', '";
	$sql .= ((isset($_COOKIE['remember_atbat'])) ? $_COOKIE['remember_atbat'] : '') . "')";
		
	$id = $db->query($sql)->fetchAll();
	$user_id = $id[0]['user_id'];
	
	// grab player data based on url hash
	if(isset($_GET['id']) && $_GET['id'] != '') {
	
		if(filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
		
			$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
			
			// gather player info
			$sql = "SELECT players.player_id, players.team_id, rosters.roster_id, SUBSTRING_INDEX(players.name, ' ', 1) AS first_name, SUBSTRING_INDEX(players.name, ' ', -1) AS last_name, players.number, players.position, players.bats, players.throws, TRUNCATE((players.height / 12), 0) AS feet, MOD(players.height, 12) AS inches, players.weight, players.dob, rosters_players.rp_id, players.player_img FROM players LEFT JOIN rosters_players ON rosters_players.player_id = players.player_id LEFT JOIN rosters ON rosters.roster_id = rosters_players.roster_id WHERE players.player_id = " . $id;
			
			try {
			
				$stmt = $db->prepare($sql);
				$stmt->execute();
				
				// bind column names
				$stmt->bindColumn(1, $p_id);
				$stmt->bindColumn(2, $team_id);
				$stmt->bindColumn(3, $roster_id);
				$stmt->bindColumn(4, $first_name);
				$stmt->bindColumn(5, $last_name);
				$stmt->bindColumn(6, $number);
				$stmt->bindColumn(7, $position);
				$stmt->bindColumn(8, $bats);
				$stmt->bindColumn(9, $throws);
				$stmt->bindColumn(10, $feet);
				$stmt->bindColumn(11, $inches);
				$stmt->bindColumn(12, $weight);
				$stmt->bindColumn(13, $dob);
				$stmt->bindColumn(14, $rp_id);
				$stmt->bindColumn(15, $player_img);
				
				$stmt->fetch(PDO::FETCH_BOUND);
			
			} catch(PDOException $e) {
			
				$message[] = '<p class="error">DB Error: ' . $e->getMessage() . '</p>';
				$error = true;
			
			}
			
		}else{
		
			$message[] = '<p class="error">Cannot validate player info.</p>';
			$error = true;
		
		}
	
	}else{
	
		$message[] = '<p class="error">Cannot find player id.</p>';
		$error = true;
		
	}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="http://localhost/atbat/html/" />
	<meta charset="utf-8">
	<title>AtBat :: View Player Details</title>
	<meta name="description" content="">
	<meta name="author" content="Sean Mullin, http://parametercontraption.com">
	<!--  Mobile Viewport Fix j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag -->
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
	<link rel="shortcut icon" href="images/design/favicon.ico">
	<link rel="apple-touch-icon" href="images/design/apple-touch-icon.png">
	<link rel="stylesheet" href="css/screen.css">
	<!-- For the less-enabled mobile browsers like Opera Mini --><script src="js/modernizr-1.5.min.js"></script>
</head>
<body>
	<?php require_once 'include/header.html'; ?>
    <section id="content">
    	<h1>Player Details</h1>
        <p>Below is the details and statistics for <em><?php echo $first_name . ' ' . $last_name; ?></em> as scored by you.</p>
        <div id="hcard-<?php echo strtoupper($first_name . '-' . $last_name); ?>" class="vcard">
        	<img src="player_images/<?php echo $player_img; ?>" alt="photo of <?php echo $first_name . ' ' . $last_name; ?>" class="photo"/>
            <a class="fn n" href="edit-player/<?php echo $p_id; ?>" title="Edit This Player"><span class="given-name"><?php echo $first_name; ?></span> <span class="family-name"><?php echo $last_name; ?></span></a>
            <?php
			
				$sql = "SELECT * FROM teams WHERE team_id = " . $team_id;
				$team = $db->query($sql)->fetch(PDO::FETCH_ASSOC);
			
			?>
            <a href="<?php echo $team['url']; ?>" class="url org"><?php echo $team['city'] . ' ' . $team['name']; ?></a>
            <ul>
            	<li><strong>Bats: </strong><?php echo $bats; ?></li>
                <li><strong>Throws: </strong><?php echo $throws; ?></li>
                <li><strong>Height: </strong><?php echo $feet . "'-" . $inches . '"'; ?></li>
                <li><strong>Weight: </strong><?php echo $weight; ?>lbs.</li>
                <li><strong>Date of Birth: </strong><?php echo date('m/d/Y', strtotime($dob)); ?></li>
            </ul>
            <table cellspacing="0" summary="Shows details and statistics for this player">
                <caption>Stats</caption>
                <tr>
                    <th>#</th>
                    <th>Position</th>
                    <th>Bats</th>
                    <th>Throws</th>
                    <th>Games</th>
                    <th>AB</th>
                    <th>1B</th>
                    <th>2B</th>
                    <th>3B</th>
                    <th>HR</th>
                    <th>RBI</th>
                    <th>BB</th>
                    <th>INTBB</th>
                    <th>K</th>
                    <th>SB</th>
                    <th>CS</th>
                    <th>AVG</th>
                    <th>SLG</th>
                    <th>OPS</th>
                </tr>
                <?php
				
                	$sql = "SELECT * FROM player_stats WHERE player_id = " . $p_id;
					$player = $db->query($sql)->fetch(PDO::FETCH_ASSOC); 
					
				?>
                <tr>
                    <td><?php echo $number; ?></td>
                    <td>
                    <?php
						
						$sql = "SELECT code FROM positions WHERE position_id = " . $position;
						$pos = $db->query($sql)->fetch(PDO::FETCH_ASSOC);
						
						echo $pos['code'];
						
					?>	
                    </td>
                    <td><?php echo $bats; ?></td>
                    <td><?php echo $throws; ?></td>
                    <td><?php echo $player['games']; ?></td>
                    <td><?php echo $player['atbats']; ?></td>
                    <td><?php echo $player['singles']; ?></td>
                    <td><?php echo $player['doubles']; ?></td>
                    <td><?php echo $player['triples']; ?></td>
                    <td><?php echo $player['home_runs']; ?></td>
                    <td><?php echo $player['rbi']; ?></td>
                    <td><?php echo $player['walks']; ?></td>
                    <td><?php echo $player['int_walks']; ?></td>
                    <td><?php echo $player['strikeouts']; ?></td>
                    <td><?php echo $player['stolen_bases']; ?></td>
                    <td><?php echo $player['caught_stealing']; ?></td>
                    <td><?php echo $player['batting_avg']; ?></td>
                    <td><?php echo $player['slg_avg']; ?></td>
                    <td><?php echo $player['obp']; ?></td>
                </tr>
                <tfoot>
                    <tr>
                        <td colspan="19">Player stats reflect only the games you have scored and not the player's actual stats.</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </section>
    <?php require_once 'include/footer.html'; ?>
</body>
</html>